package org.openfans.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.openfans.domain.Feed;
import org.openfans.feed.FeedRetriever;
import org.openfans.web.form.FeedForm;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class NewFeedController extends AbstractController {
	private FeedRetriever feedRetriever;

	public void setFeedRetriever(FeedRetriever feedRetriever) {
		this.feedRetriever = feedRetriever;
	}

	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String url = request.getParameter("url");
		// 点击链接进入，跳转到新增页面
		if (url==null) {
			FeedForm f=new FeedForm();
			f.setTagStr(request.getParameter("tag"));
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("feedForm", f);
			return new ModelAndView("public/newFeedForm",map);
		}
		String tag=request.getParameter("tag");
		String type=request.getParameter("type");
		String name = request.getParameter("name");
		String site = request.getParameter("site");
		if (site != null && !"".equals(name)) {
			if ("1".equals(site)) {
				url = "http://www.blogjava.net/" + name + "/Rss.aspx";
			} else if ("2".equals(site)) {
				url = "http://spaces.msn.com/" + name + "/feed.rss";
			} else if ("3".equals(site)) {
				url = "http://feeds.feedburner.com/" + name;
			}else if ("4".equals(site)) {
				url = "http://blog.csdn.net/" + name+"/Rss.aspx";
			}
		}

		
		Map<String,Object> map = new HashMap<String,Object>();

		Feed feed = feedRetriever.getFeedInfoFromUrl(url);
		FeedForm f=new FeedForm();
		PropertyUtils.copyProperties(f, feed);
		f.setTagStr(tag);
		f.setType(type);
		map.put("feedForm", f);
		return new ModelAndView("public/feedForm", map);
	}
}
